package com.yuan.socket.server;

import com.yuan.socket.data.DataService;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.Channel;
import io.netty.channel.EventLoop;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;

public class Websocket {
    public static void start(DataService dataService) {
        //定义2个线程组
        EventLoopGroup bossGroup =  new NioEventLoopGroup(1);
        EventLoopGroup workerGroup =  new NioEventLoopGroup();
        try {

              ServerBootstrap b = new ServerBootstrap();
               b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class).childHandler(new WebSocketServerInitializer(dataService));

            Channel channel = b.bind(8888).sync().channel();
            channel.closeFuture().sync();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }finally {
            bossGroup.shutdownGracefully();
            workerGroup.shutdownGracefully();
        }
    };
}
